﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameVLTK
{
    public abstract class VisibleGameEntity : GameEntity
    {
        private String _Name;

        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        protected List<MySprite> _Sprites = new List<MySprite>();

        public List<MySprite> Sprites
        {
            get
            {
                return _Sprites;
            }
            set
            {
                _Sprites = value;
                _nSprites = _Sprites.Count;
            }
        }
        protected int _nSprites;

        public int nSprites
        {
            get { return _nSprites; }
            set { _nSprites = value; }
        }

        //Tọa độ thực so với World Map
        protected Vector2 _TopLeft;

        public Vector2 TopLeft
        {
            get { return _TopLeft; }
            set
            {
                _TopLeft = value;
                for (int i = 0; i < _Sprites.Count; ++i)
                {
                    _Sprites[i].TopLeft = _TopLeft;
                }

                this.Z_Index = _TopLeft.Y;
            }
        }

        //Tọa đô logic của đối tượng so với đối tượng 
        //chứa nó
        private Vector2 _Logic;

        protected Vector2 Logic
        {
            get { return _Logic; }
            set { 
                _Logic = value;
                for (int i = 0; i < _Sprites.Count; ++i)
                {
                    _Sprites[i].Logic = _Logic;
                }

            }
        }

        protected Vector2 _Size;

        public Vector2 Size
        {
            get { return _Size; }
            set { _Size = value; }
        }

        //Vị trí Z-Index
        protected float _Z_Index;

        public float Z_Index
        {
            get { return _Z_Index; }
            set
            {
                _Z_Index = value;
                _Z_Index+= this.Size.Y;
            }
        }

        public VisibleGameEntity()
        {
            Global.gameEntities.Add(this);
        }

        public virtual bool IsClicked(MouseState mouseState)
        {
            if (mouseState.LeftButton == ButtonState.Pressed)
                if ((mouseState.X >= TopLeft.X && mouseState.X <= TopLeft.X + Size.X) &&
                   (mouseState.Y >= TopLeft.Y && mouseState.Y <= TopLeft.Y + Size.Y))
                    return true;

            return false;
        }

        public virtual void Update(GameTime gameTime)
        {
            for (int i = 0; i < _nSprites; i++)
                _Sprites[i].Update(gameTime);
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _nSprites; i++)
                _Sprites[i].Draw(gameTime, spriteBatch);
        }


        /// <summary>
        /// Clone ra một đối tượng
        /// </summary>
        public virtual VisibleGameEntity Clone()
        {
            return null;
        }
    }
}
